<?php
namespace submodule\modules\category\components;

use Yii;
use yii\db\Schema;
use yii\db\Migration;

class Uninstaller extends Migration
{
	public $controller;
	
	public function __construct($controller){
		$this->controller = $controller;
		
	}
    //安装
	//返回添加后的modelid
    public function install($option)
    {
		//建表
        Yii::$app->db->createCommand()->createTable('{{%'.$option['tablename'].'}}', [
            'id' => 'pk',
            'parentid'=> "smallint(5) NOT NULL default '0' COMMENT '父栏目ID'",    //product type id
            'name'=> "varchar(80) NOT NULL default '' COMMENT '名称'",
            'listorder'=> "tinyint(3) NOT NULL default '0' COMMENT '排序'",
            'status'=> "tinyint(1) NOT NULL default '1' COMMENT '状态0为不显示'",
            'addtime'=> "int(10) NOT NULL default '0' COMMENT '添加时间'",
            'updatetime'=> "int(10) NOT NULL default '0' COMMENT '更新时间'",
            'icon'=> "varchar(150) COMMENT '图标'",
			'children'=> "int(10) COMMENT '栏目树'",
			'depth'=> "varchar(250) default '0' COMMENT '栏目树'",
            'intro'=> "text COMMENT '介绍'"
        ],'ENGINE InnoDB DEFAULT CHARSET utf8')->execute();
		
		
		//记录字段
		$fields = ['modelid', 'name', 'field', 'formtype', 'fieldtype', 'fieldlength', 'listorder', 'disabled', 'primarytable', 'required', 'setting', 'param', 'api', 'allowinput'];
		
		$rows = [
		     [$option['modelid'], 'ID', 'id', 'primarykey', 'int', 10, 0, 0, 2, 1, '', '', '', 0], 
			 [$option['modelid'], '父栏目ID', 'parentid', 'category', 'int', 10, 0, 0, 2, 1, '', '', '', 1], 
			 [$option['modelid'], '名称', 'name', 'text', 'varchar', 80, 0, 0, 1, 1, '', '', '', 1], 
			 [$option['modelid'], '排序', 'listorder', 'text', 'int', 10, 0, 0, 1, 1, '{"defaultvalue":"0"}', '', '', 0], 
			 [$option['modelid'], '状态', 'status', 'switch', 'int', 10, 0, 0, 1, 1, '', '', '', 1], 
			 [$option['modelid'], '添加时间', 'addtime', 'text', 'int', 10, 0, 0, 1, 1, '', '', '', 0], 
			 [$option['modelid'], '更新时间', 'updatetime', 'text', 'int', 10, 0, 0, 1, 1, '', '', '', 0], 
			 [$option['modelid'], '图标', 'icon', 'image', 'varchar', 150, 0, 0, 1, 0, '', '', '', 1],
			 [$option['modelid'], '子栏目', 'children', 'text', 'int', '10', 0, 0, 1, 0, '', '', '', 0],  
			 [$option['modelid'], '栏目树', 'depth', 'text', 'text', '', 0, 0, 1, 0, '', '', '', 0], 
			 [$option['modelid'], '介绍', 'intro', 'textarea', 'text', 1000, 0, 0, 1, 0, '', '', '', 1]
	    ];
		Yii::$app->db->createCommand()->batchInsert($option['field_tablename'], $fields, $rows)->execute();
		return [$option['modelid']];
    }
    
	
	//卸载
    public function uninstall()
    {
        $this->dropTable('{{store_product}}');
    }
}
